package com.example.origin.finance.utils;

import lombok.experimental.UtilityClass;

import java.math.BigDecimal;
import java.math.RoundingMode;

@UtilityClass
public class CalcUtil {


    public BigDecimal multiply(Object a, Object b) {
        return getBigDecimal(a).multiply(getBigDecimal(b)).setScale(3, RoundingMode.HALF_UP);
    }

    public Double doubleMultiply(Object a, Object b) {
        return multiply(a, b).doubleValue();
    }

    public Double doubleDivide(Object a, Object b) {
        return divide(a, b).doubleValue();
    }

    public Long longMultiply(Object a, Object b) {
        return multiply(a, b).longValue();
    }

    public Long longDivide(Object a, Object b) {
        return divide(a, b).longValue();
    }

    private static BigDecimal getBigDecimal(Object a) {
        BigDecimal bigDecimal;
        if (a instanceof Long) {
            bigDecimal = BigDecimal.valueOf((Long) a);
        } else if (a instanceof Double) {
            bigDecimal = BigDecimal.valueOf((Double) a);
        } else if (a instanceof BigDecimal) {
            bigDecimal = (BigDecimal) a;
        } else if (a instanceof Integer) {
            bigDecimal = BigDecimal.valueOf((Integer) a);
        } else {
            throw new RuntimeException();
        }
        return bigDecimal.setScale(3, RoundingMode.HALF_UP);
    }


    public BigDecimal divide(Object a, Object b) {
        return getBigDecimal(a).divide(getBigDecimal(b), 3, RoundingMode.HALF_UP)
                .setScale(3, RoundingMode.HALF_UP);
    }


    public String strOf(Object obj){
        return getBigDecimal(obj).setScale(3, RoundingMode.HALF_UP).toString();
    }



}
